package test
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2FixtureDef;
	
	import assets.avatar.ac.AC10000Asset;
	
	import com.waitingsnow.core.debug.Logger;
	import com.waitingsnow.core.display.player.BmpDataPlayer;
	import com.waitingsnow.core.util.display.BmpDataUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.AccelerometerEvent;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.sensors.Accelerometer;
	
	import instances.ExtBox2DInstance;
	import instances.ExtCircleInstance;
	import instances.ExtPolygonInstance;
	
	import noiseandheat.ane.box2d.Box2D;
	import noiseandheat.ane.box2d.data.b2BodyProxy;
	
	public class Box2D_ANE_Example extends Sprite
	{
		private var b2d:Box2D;
		/*private var objects:Vector.<ExtBox2DInstance>;*/
		//private var canvas:Bitmap;
		private var container:Sprite;
		
		/*[Embed (source="assets/circle.png")]
		private var circleClass:Class;*/
		/*private var circleBitmapData:BitmapData;*/
		
		[Embed (source="assets/rect.png")]
		private var rectClass:Class;
		private var rectBitmapData:BitmapData;
		
		private var gravityX:Number = 0;
		private var gravityY:Number = 0;
		private var speed:int = 160;
		
		private var avatarAsset:AC10000Asset;
		private var avatarBmp:BmpDataPlayer;
		private var avatarObj:ExtBox2DInstance;
		
		//==========================================================================
		//  Constructor
		//==========================================================================
		/**
		 * Constructs a new <code>Application</code> instance.
		 * 
		 */
		public function Box2D_ANE_Example()
		{
			initAvatar();
			create();
		}
		private function initAvatar():void
		{
			avatarAsset = new AC10000Asset();
			avatarBmp = new BmpDataPlayer();
			avatarBmp.repeatCount = 0;
			avatarBmp.bmpDataArr = BmpDataUtil.getBmpDataArr(avatarAsset);
		}
		
		private function create():void{
			/*circleBitmapData = Bitmap(new circleClass()).bitmapData;
			rectBitmapData = Bitmap(new rectClass()).bitmapData;*/
			
			b2d = new Box2D();
			var accelerometer:Accelerometer = new Accelerometer();
			accelerometer.addEventListener(AccelerometerEvent.UPDATE, accelUpdateHandler);
			updateGravity();
			container = new Sprite();
			addChild(container);
			createBorder();
			createObjects();
			avatarBmp.play();
			
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		private function accelUpdateHandler(e:AccelerometerEvent):void{
			/*Logger.debug("gravityX: ", gravityX, "gravityY: ", gravityY);*/
			gravityX = -e.accelerationX;
			gravityY = e.accelerationY;
		}
		private function updateGravity():void{
			b2d.setWorldGravity(new b2Vec2(speed*gravityX,speed*gravityY));
		}
		private function enterFrameHandler(e:Event):void{
			updateGravity();
//			b2d.worldStep(1/60, 8, 3);
			b2d.worldStep(1/20, 24, 9);
			updateView();
		}
		private function createBorder():void{
			var w:int = 500;
			var h:int = 800;
			var shape:Shape = new Shape();
			shape.graphics.beginFill(0x666666,1);
			var upBox:ExtPolygonInstance = new ExtPolygonInstance();
			upBox.createBoxBody(b2d,30,0,-10,w,10,0);
			shape.graphics.drawRect(0,-10,w,10);
			var downBox:ExtPolygonInstance = new ExtPolygonInstance();
			downBox.createBoxBody(b2d,30,0,(h),w,10,0);
			shape.graphics.drawRect(0,h,w,10);
			var leftBox:ExtPolygonInstance = new ExtPolygonInstance();
			leftBox.createBoxBody(b2d,30,-10,0,10,h,0);
			shape.graphics.drawRect(-10,0,10,h);
			var rightBox:ExtPolygonInstance = new ExtPolygonInstance();
			rightBox.createBoxBody(b2d,30,(w),0,10,h,0);
			shape.graphics.drawRect(w,0,10,h);
			
			shape.graphics.endFill();
			addChild(shape);
		}
		
		private function createObjects():void
		{
			avatarObj = new ExtCircleInstance();
			avatarObj.type = "circle";
			ExtCircleInstance(avatarObj).createBoxBody(b2d,30,Math.random() * (640 - 300), Math.random() * (960 - 300), 
				20, /*Math.random() * 180 - 90*/0, 1 + Math.random(),2,.05,true);
			avatarBmp.x = avatarObj.getPosition().x;
			avatarBmp.y = avatarObj.getPosition().y;
			avatarBmp.rotation = avatarObj.getRotation();
			container.addChild(avatarBmp);
			
			/*objects = new Vector.<ExtBox2DInstance>;
			for(var i:int=0;i<30;i++){
				var object:ExtBox2DInstance;
				var view:Sprite = new Sprite();
				var bitmap:Bitmap;
				if(i<15){
					object = new ExtCircleInstance();
					object.type = "circle";
					ExtCircleInstance(object).createBoxBody(b2d,30,Math.random() * 640, Math.random() * 960, 
						25, Math.random() * 360, 1 + Math.random(),.5,.1,true);
					bitmap = new circleClass();
				}else{
					object = new ExtPolygonInstance();
					object.type = "rect";
					ExtPolygonInstance(object).createBoxBody(b2d,30,Math.random() * 640, Math.random() * 960, 
						25,25,Math.random() * 360,1 + Math.random(),.5,.1,true);
					bitmap = new rectClass();
				}
				objects.push(object);
				view.name = String(object.body.id);
				container.addChild(view);
				view.x = object.getPosition().x;
				view.y = object.getPosition().y;
				view.rotation = object.getRotation();
				view.addChild(bitmap);
				bitmap.x = - bitmap.width/2;
				bitmap.y = - bitmap.height/2;
			}*/
		}
		
		private function updateView():void
		{
			avatarBmp.x = avatarObj.getPosition().x;
			avatarBmp.y = avatarObj.getPosition().y;
			avatarBmp.rotation = avatarObj.getRotation();
			
			/*for each(var object:ExtBox2DInstance in objects){
				var view:Sprite = container.getChildByName(String(object.body.id)) as Sprite;
				if(view){
					view.x = object.getPosition().x;
					view.y = object.getPosition().y;
					view.rotation = object.getRotation();
				}
			}*/
		}
	}
}